package com.shiguiwu.springmybatis.app.controller;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 请求超时设置测试
 * @author: stone
 * @date: Created by 2021/8/1 22:42
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.app.controller
 */
@RestController
@Slf4j
public class TimeOutController {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 通过属性 connection-timeout: 180000
     * @return
     */
    @GetMapping("/timeOutProp")
    public String timeOutProp(HttpServletRequest request) throws InterruptedException {
        long l = System.currentTimeMillis();
        log.info("请求进入====================> {}", l);
        //TimeUnit.SECONDS.sleep(75);
        String requestedSessionId = String.valueOf(System.nanoTime());
        log.info("requestedSessionId====================>{}",requestedSessionId);
        String val = "0";

        stringRedisTemplate.opsForValue().set("timeout",val);

        while (StrUtil.isBlank(val) || "0".equalsIgnoreCase(val)) {
            //log.info("死循环====================> {}" ,val);
            val = stringRedisTemplate.opsForValue().get("timeout");

        }
        l = (System.currentTimeMillis() - l) / 1000;
        log.info("请求进入====================> end ==> {}" ,l);
        return "成功！";
    }
}
